package com.shenmazong.servershopuser.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.comm.utils.JwtUtil;
import com.shenmazong.comm.utils.ResultResponse;
import com.shenmazong.comm.vo.AdminVO;
import com.shenmazong.servershopuser.user.entity.TbAdmin;
import com.shenmazong.servershopuser.user.mapper.TbAdminMapper;
import com.shenmazong.servershopuser.user.service.ITbAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author 军哥
 * @since 2021-07-15
 */
@Service
@Slf4j
public class TbAdminServiceImpl extends ServiceImpl<TbAdminMapper, TbAdmin> implements ITbAdminService {

    @Autowired
    TbAdminMapper tbAdminMapper;

    @Override
    public ResultResponse add(AdminVO admin) {
        //--1 判断是否存在
        TbAdmin one = tbAdminMapper.selectOne(
                new QueryWrapper<TbAdmin>().lambda().eq(TbAdmin::getAdminName, admin.getAdminName()).last("limit 1")

        );
        if(one != null) {
            return ResultResponse.FAILED(-1, "管理员已经存在");
        }

        //--2 添加?
        TbAdmin tbAdmin = new TbAdmin();
        try {
            BeanUtils.copyProperties(tbAdmin, admin);

            int insert = tbAdminMapper.insert(tbAdmin);
            log.info("insert:"+insert);

            //--3 判断添加成功
            if(tbAdmin.getAdminId() == null) {
                return ResultResponse.FAILED(-1, "添加管理员失败");
            }
            admin.setAdminId(tbAdmin.getAdminId());

            return ResultResponse.SUCCESS(admin);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return ResultResponse.FAILED(-1, "添加管理员失败");
    }

    @Override
    public ResultResponse set(AdminVO admin) {
        //--1 判断是否存在
        TbAdmin one = tbAdminMapper.selectById(admin.getAdminId());
        if(one == null) {
            return ResultResponse.FAILED(-1, "管理员不存在");
        }

        //--2 修改
        TbAdmin tbAdmin = new TbAdmin();
        try {
            BeanUtils.copyProperties(tbAdmin, admin);

            tbAdminMapper.updateById(tbAdmin);

            //--3 判断添加成功
            TbAdmin admin1Ok= tbAdminMapper.selectById(tbAdmin.getAdminId());
            BeanUtils.copyProperties(admin, admin1Ok);

            return ResultResponse.SUCCESS(admin);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return ResultResponse.FAILED(-1, "修改管理员失败");
    }

    @Override
    public ResultResponse del(AdminVO admin) {
        //--1 判断是否存在
        TbAdmin one = tbAdminMapper.selectById(admin.getAdminId());
        if(one == null) {
            return ResultResponse.FAILED(-1, "管理员不存在");
        }

        //--2 删除
        tbAdminMapper.deleteById(admin.getAdminId());

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse listByPage(Integer from, Integer size) {

        Page<TbAdmin> page = new Page<>(from, size);

        Page<TbAdmin> tbAdminPage = tbAdminMapper.selectPage(page, null);

        List<TbAdmin> records = tbAdminPage.getRecords();

        //-- begin
        ArrayList<AdminVO> adminVOS = null;
        try {
            adminVOS = new ArrayList<>();
            for (TbAdmin record : records) {
                AdminVO adminVO = new AdminVO();
                BeanUtils.copyProperties(adminVO, record);

                adminVOS.add(adminVO);
            }

            Page<AdminVO> adminVOPage = new Page<>(from, size);
            adminVOPage.setTotal(tbAdminPage.getTotal());
            adminVOPage.setPages(tbAdminPage.getPages());
            adminVOPage.setRecords(adminVOS);

            return ResultResponse.SUCCESS(adminVOPage);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //--end

        return ResultResponse.FAILED(-1, "获取管理员列表失败");
    }

    @Override
    public ResultResponse login(AdminVO admin) {
        //--1 判断用户是否存在
        LambdaQueryWrapper<TbAdmin> wrapper = new QueryWrapper<TbAdmin>().lambda().eq(TbAdmin::getAdminName, admin.getAdminName()).last("limit 1");
        TbAdmin tbAdmin = tbAdminMapper.selectOne(wrapper);
        if(tbAdmin == null) {
            return ResultResponse.FAILED(-1, "用户不存在");
        }

        //--2 判断密码是否正确
        if(!tbAdmin.getAdminPass().equals(admin.getAdminPass())) {
            return ResultResponse.FAILED(-1, "密码错误");
        }

        //--3 返回token
        String token = JwtUtil.makeToken(tbAdmin.getAdminId(), tbAdmin.getAdminName(), "admin");

        //--4 生成返回值
        try {
            BeanUtils.copyProperties(admin, tbAdmin);
            admin.setToken(token);

            admin.setAdminPass(null);

            return ResultResponse.SUCCESS(admin);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return ResultResponse.FAILED(-1, "登录失败");
    }


}
